//
//  HomeRecommendTableViewCell.swift
//  FM
//
//  Created by x on 2017/8/31.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit

protocol HomeRecommendTableViewCellDelegate:NSObjectProtocol {
    
    //播放FM
    func homeRecommendTableViewCellplayFM(currentIndex:Int, totalFM:[HomeFM])
}

class HomeRecommendTableViewCell: UITableViewCell {
    
    weak var delegate:HomeRecommendTableViewCellDelegate?

    var models:[HomeFM]? {
        didSet {
            collectionView.reloadData()
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //1.添加子控件
        
        contentView.addSubview(collectionView)
        //2.布局子控件
        var cons:[NSLayoutConstraint] = [NSLayoutConstraint]()
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[collectionView]-0-|", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: ["collectionView":collectionView])
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[collectionView]-0-|", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: ["collectionView":collectionView])
        
        contentView.addConstraints(cons)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: -- 懒加载
    fileprivate lazy var recommendLayout:RecommendLayout = RecommendLayout()
    
    private lazy var collectionView:UICollectionView = {
        let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: self.recommendLayout)
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        collectionView.backgroundColor = UIColor.clear
        collectionView.delegate = self
        collectionView.dataSource = self
        //注册cell
        collectionView.register(HomeRecommendCell.self, forCellWithReuseIdentifier: HomeRecommendCellIdentifier)
        return collectionView
    }()
}

private let HomeRecommendCellIdentifier = "HomeRecommendCellIdentifier"

extension HomeRecommendTableViewCell:UICollectionViewDelegate,UICollectionViewDataSource {

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return  models?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let  cell = collectionView.dequeueReusableCell(withReuseIdentifier: HomeRecommendCellIdentifier, for: indexPath) as! HomeRecommendCell
            cell.homeFMS = models?[indexPath.row]
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        //这里需要跳转vc
        delegate?.homeRecommendTableViewCellplayFM(currentIndex: indexPath.item, totalFM: models!)
    }
    
    
    
}

class RecommendLayout: UICollectionViewFlowLayout {
    override func prepare() {
        super.prepare()
        
        //设置属性
        let w = (collectionView!.bounds.width-50)/3
        //let h = 80
        
        itemSize = CGSize(width: w, height: 80)
        minimumLineSpacing = 0
        minimumInteritemSpacing = 0
        scrollDirection = UICollectionViewScrollDirection.vertical//设置滚动方向是垂直方向
        
        collectionView?.contentInset = UIEdgeInsetsMake(0, 5, 0, 5)
        collectionView?.alwaysBounceVertical = true
    }
}

class HomeRecommendCell: UICollectionViewCell {
    
    var homeFMS:HomeFM? {
        didSet {
            iconImage.sd_setImage(with: homeFMS!.iconImageURL)
            titleLable.text = homeFMS?.title
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //1.添加子控件
        contentView.addSubview(iconImage)
        contentView.addSubview(titleLable)
        
        //2.布局子控件
        var cons:[NSLayoutConstraint] = [NSLayoutConstraint]()
        let tt = ["iconImage":iconImage,"titleLable":titleLable] as [String: Any]
        
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[iconImage]-0-|", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: tt)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[iconImage(60)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: tt)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[titleLable]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: tt)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[iconImage]-0-[titleLable]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: tt)
        
        contentView.addConstraints(cons)
        
    }
    
    //MARK: -- 懒加载
    private lazy var iconImage:UIImageView = UIImageView.layoutImageView()
       
    private lazy var titleLable:UILabel = {
        let tt = UILabel.lableWith(title: "", titleColor: UIColor.black, fontSize: 10)
        tt.preferredMaxLayoutWidth = self.bounds.width
        tt.numberOfLines = 0
        
        return tt
    }()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}













